<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/6/26 0026
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\admin\model\AuthRule as AuthRules;
use app\common\controller\AdminBaseController;
use Jrk\Tree;
use app\common\model\tang\GroupConfig as Group;

class GroupConfig extends AdminBaseController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new Group();
    }

    /**
     * @return mixed|string|\think\response\Json
     * @throws \think\db\exception\DbException
     * @author: Hhy <jackhhy520@qq.com>
     * @date: 2020/6/30 0030
     * @describe:添加编辑
     */
    public function add()
    {
        if (IS_AJAX) {
            $data = $this->request->post();
            $data['merchant_id'] = $data['select'];
            return $this->model->doAll($data);
        }
        $id = $this->request->param("id/d");//父id
        $info = $this->model->find($id);

        //获取菜单列表
        if(isset($info['merchant_id'])&&!empty($info['merchant_id'])){
            $task = $this->getTask();
            //循环匹配
            $taskInfo = explode(',',$info['merchant_id']);
            foreach ($taskInfo as $k=>$v){
                foreach ($task as $kk=>$vv){
                    if($v==$vv['value']){
                        $task[$kk]['selected']=true;
                    }
                }
            }
        }else{
            $task = $this->getTask();
        }
        $this->assign('task',$task);
        $this->assign(compact("info", "id"));
        return $this->fetch();
    }
    /**
     * 任务选项
     */
    public function getTask(){
        $task = \app\common\model\tang\Merchant::field('id,name,contractNo')->whereNotIn('status',[40,50])->select();
        foreach ($task as $k=>$v){
            $task[$k]['name'] = $v['name'].'-'.$v['contractNo'];
            $task[$k]['value'] =$v['id'];
            $task[$k]['selected'] = false;
        }
        return $task;
    }

}